package com.util;


import android.Manifest;
import android.app.Activity;
import android.content.pm.PackageManager;
import android.os.Build;

public class PermissionUtils {

	private static String[] PERMISSIONS_CAMERA_AND_STORAGE = {
			Manifest.permission.WRITE_EXTERNAL_STORAGE,
			Manifest.permission.READ_EXTERNAL_STORAGE,
			Manifest.permission.READ_PHONE_STATE,
			Manifest.permission.CAMERA};

	/**
	 * 解决安卓6.0以上版本不能读取外部存储权限的问题
	 *
	 * @param activity
	 * @param requestCode
	 * @return
	 */
	public static boolean isGrantExternalRW(Activity activity, int requestCode) {
		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {

			int storagePermission = activity.checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE);
			int storagePermission2 = activity.checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE);
			int storagePermission3 = activity.checkSelfPermission(Manifest.permission.READ_PHONE_STATE);
			int cameraPermission = activity.checkSelfPermission(Manifest.permission.CAMERA);
			//检测是否有权限，如果没有权限，就需要申请
			if (storagePermission != PackageManager.PERMISSION_GRANTED ||
					storagePermission2 != PackageManager.PERMISSION_GRANTED||
					storagePermission3 != PackageManager.PERMISSION_GRANTED||
					cameraPermission != PackageManager.PERMISSION_GRANTED) {
				//申请权限
				activity.requestPermissions(PERMISSIONS_CAMERA_AND_STORAGE, requestCode);
				//返回false。说明没有授权
				return false;
			}
		}
		//说明已经授权
		return true;
	}
}
